package com.yuanfengerp.salesReturn.service.impl;

import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.salesReturn.dao.ISalesReturnDao;
import com.yuanfengerp.salesReturn.model.salesReturn;
import com.yuanfengerp.salesReturn.service.ISalesReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SalesReturnServiceImpl implements ISalesReturnService {
    @Autowired
    private ISalesReturnDao iSalesReturnDao;
    @Override
    public Result<salesReturn> insertReturnOrder(salesReturn salesReturn) {
        int i = iSalesReturnDao.insertSalesReturn(salesReturn);
        if(i > 0){
            return Result.success(salesReturn);
        }else {
            return Result.failure(500, "添加退货单失败");
        }
    }

    @Override
    public Result<salesReturn> updateReturnOrderAgree(String[] salesReturnNumbers) {
        for (String salesReturnNumber : salesReturnNumbers) {
            int status = iSalesReturnDao.selectSalesReturnStatus(salesReturnNumber);
            if (status == 1 || status == 2) {
                return Result.failure(400, "当前订单状态为" + status + "，无法进行审核操作");
            } else {
                int updateNum = iSalesReturnDao.updateSalesReturnAgree(salesReturnNumber);
                if (updateNum <= 0) {
                    return Result.failure(500, "退货单审核失败");
                }
            }
        }
        return Result.success();
    }

    @Override
    public Result<salesReturn> updateReturnOrderRefuse(String[] salesReturnNumbers) {
        int successNum = 0;
        int failNum = 0 ;
        for (String salesReturnNumber : salesReturnNumbers) {
            int status = iSalesReturnDao.selectSalesReturnStatus(salesReturnNumber);
            if (status == 1 || status == 2) {
                failNum ++ ;
            } else {
                int updateNum = iSalesReturnDao.updateSalesReturnRefuse(salesReturnNumber);
                if (updateNum > 0) {
                    successNum++;
                } else {
                    return Result.failure(500, "退货单审核失败");
                }
            }
        }
        return Result.success();
    }

    @Override
    public Result<List<salesReturn>> selectAllReturnOrder(Map<String, Object> condition) {
        List<salesReturn> salesReturns = iSalesReturnDao.selectAllSalesReturn(condition);
        if (!salesReturns.isEmpty()) {
            return Result.success(salesReturns);
        }else {
            return Result.failure(400, "没有相关数据");
        }
    }

    @Override
    public Result<salesReturn> selectReturnOrder(String salesReturnNumber) {
        salesReturn salesReturn = iSalesReturnDao.selectSalesReturn(salesReturnNumber);
        if (salesReturn != null) {
            return Result.success(salesReturn);
        }else {
            return Result.failure(400, "没有相关数据");
        }
    }
}
